﻿
using Newtonsoft.Json;
using RTW.OA.Areas.System.Servers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Controllers
{
    public class FlowController : Controller
    {
	      private FlowServers flowServers;

		    public FlowController()
			{
				flowServers = new FlowServers();
			}


		// GET: System/Flow
        public ActionResult Index()
        {
            ViewBag.TYPEIDData = JsonConvert.SerializeObject(flowServers.GetTYPEID());
            ViewBag.TYPEData = JsonConvert.SerializeObject(flowServers.GetTYPE());
            return View();
        }

		// GET: System/Flow/Edit/5
        public ActionResult Edit(int? id)
        {
			ViewBag.TYPEIDData = flowServers.GetTYPEID();
            ViewBag.TYPEData = flowServers.GetTYPE();
            ViewBag.FORMIDData = flowServers.GetFORMID();
            var model = new wf_flowinfo();
            if (id != null)
            {
                ViewBag.Title = "编辑流程";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建流程";
            }
            return View(model);
        }

		// POST: System/Flow/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, wf_flowinfo model)
        {
			ViewBag.TYPEIDData = flowServers.GetTYPEID();
            ViewBag.TYPEData = flowServers.GetTYPE();
            ViewBag.FORMIDData = flowServers.GetFORMID();
            try
            {
                if (id == null || id == 0)
                {
                    flowServers.Add(model);
                    ViewBag.msg = "新增流程信息成功！";
                    model = new wf_flowinfo();
                }
                else
                {
                    var m = flowServers.GetModelById(id.Value.ToString());
                    m.TYPE = model.TYPE;
                    m.TYPEID = model.TYPEID;
                    m.FORMID = model.FORMID;
                    m.NAME = model.NAME;
                    m.STATUS = model.STATUS;
                    flowServers.Update(m);
                    ViewBag.msg = "修改流程信息成功！";

                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }


        // GET: System/Step
        public ActionResult Flowdesign(int? id)
        {
            ViewBag.NAMEData = "";
            var model = new wf_flowinfo();
            if (id != null)
            {              
                ViewBag.Title = "编辑下一步骤";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建下一步骤";
            }
            return View(model);
        }
        // POST: System/Step/Edit/5
        [HttpPost]
        public ActionResult Flowdesign(int? id, wf_flowinfo model)
        {
          
            try
            {
                if (id == null || id == 0)
                {
                    flowServers.Add(model);
                    ViewBag.msg = "新增下一步骤信息成功！";
                    model = new wf_flowinfo();
                }
                else
                {
                    var m = flowServers.GetModelById(id.Value.ToString());
                    
                    flowServers.Update(m);
                    ViewBag.msg = "修改下一步骤信息成功！";
                }
            }
            catch 
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: System/Flow/Delete/5
        public ActionResult Delete(int id)
        {
		    if (id != 0)
            {
                flowServers.Del(id.ToString());
                ViewBag.msg = "删除流程信息成功！";                   
            }    
            return Content("");
        }


		 #region //GetData
		public string GetTableJsonResult()
        {
            var list = flowServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

		public wf_flowinfo GetModel(string id)
        {
            var model = flowServers.GetModelById(id);
            return model;
        }        

        #endregion //GetData
    }
}